package cc.comac.mom.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.QualityAsrFeaturesService;
import cc.comac.mom.bean.QualityAsrFeatures;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.util.ExcelUploadUtil;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/QualityAsrFeatures")
public class QualityAsrFeaturesController {

	@Autowired
	private QualityAsrFeaturesService equipAsrFeaturesService;
	@Autowired
	private OSSService oSSService;
	
	@ApiOperation("单个新增asr特征库数据")
	@RequestMapping(value = "/insert",method = RequestMethod.POST)
	public ResultBean insert(@RequestBody QualityAsrFeatures equipAsrFeatures) {
		ResultBean insert = equipAsrFeaturesService.insert(equipAsrFeatures);
		return insert;
	}

	@ApiOperation("批量新增asr特征库数据")
	@RequestMapping(value = "/insertBatch",method = RequestMethod.POST)
	public Object insertBatch(@RequestBody List<QualityAsrFeatures> equipAsrFeaturesList) {
		ResultBean insertBatch = equipAsrFeaturesService.insertBatch(equipAsrFeaturesList);
		return insertBatch;
	}
	@ApiOperation("更新asr特征库数据")
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public ResultBean update(@RequestBody QualityAsrFeatures equipAsrFeatures) {
			ResultBean update = equipAsrFeaturesService.update(equipAsrFeatures);
		return update;
	}

	@ApiOperation("分页查询所有asr特征库数据")
	@RequestMapping(value = "/selectAllByPage",method = RequestMethod.POST)
	public ResultBean selectAllByPage(@RequestBody QualityAsrFeatures equipAsrFeatures) {
			ResultBean selectAllByPage = equipAsrFeaturesService.selectAllByPage(equipAsrFeatures);
		return selectAllByPage;
	}
	@ApiOperation("批量删除asr特征库数据")
	@RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
	public Object batchDelete(@RequestBody List<QualityAsrFeatures> equipAsrFeaturesList) {
			ResultBean batchDelete = equipAsrFeaturesService.batchDelete(equipAsrFeaturesList);
		return batchDelete;
	}
	
	@ApiOperation(value = "asr特征库导出数据")
	@RequestMapping("/toExportExcel")  
	public void exportExcel(HttpServletRequest request, HttpServletResponse response,@RequestBody QualityAsrFeatures obj) {
		String url = equipAsrFeaturesService.exportData(request, response, obj);
		String fileName = "ASR特征库数据";
		try {
			oSSService.export(response, url, fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation(value = "ASR特征库导出模板下载")
	@RequestMapping(value = "/TemplateDownload",method = RequestMethod.POST)
	public @ResponseBody void TemplateDownload(HttpServletResponse request, HttpServletResponse response)
			throws Exception {
		String fileName = "ASR特征库" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		ArrayList<String> listName = new ArrayList<String>();
		listName.add("材料/零件图号");
		listName.add("特征");
		listName.add("尺寸");
		listName.add("初始可靠值");
		listName.add("等级");
		listName.add("特征计数");
		ExcelUploadUtil.TemplateDownload(request, response, fileName, listName);
	}
	
	@ApiOperation(value = "ASR特征库导入数据")
	@RequestMapping(value = "/TemplateDataUpload", method = RequestMethod.POST)
	public @ResponseBody ResultBean dataUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request)
			throws IOException {
		// 文件所在的临时文件夹
		String rootPath = request.getServletContext().getRealPath("/") + "uploadExcel";
		ResultBean importEquipDevType = equipAsrFeaturesService.importEquipDevType(file, rootPath);
		return importEquipDevType;
	}
}
